﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Web.Mvc;
using MSAccess = Microsoft.Office.Interop.Access;
using MSWord = Microsoft.Office.Interop.Word;

namespace MvcMSAccessReportPDF.Controllers
{
    public class HomeController : Controller
    {
        // Author: Otto Neff | webapps-in-action.com
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetPdf()
        {
            string reportName = "SalesTotalsByAmount";
            string mdb = Server.MapPath("~/App_Data/nwind.mdb");
            string rtf = Server.MapPath("~/App_Data/" + reportName + ".rtf");
            object missingValue = System.Reflection.Missing.Value;
            MSAccess.Application app = new MSAccess.Application();
            app.OpenCurrentDatabase(mdb, true);
            app.DoCmd.OpenReport("SalesTotalsByAmount", MSAccess.AcView.acViewPreview, missingValue, missingValue, MSAccess.AcWindowMode.acWindowNormal, missingValue);

            // YOU MAY USE THIS TO PRINT TO A DEFAULT PDF PRINTING SILENT INSTEAD OF USING WORD
            // app.DoCmd.PrintOut(MsAccess.AcPrintRange.acPrintAll, Type.Missing, Type.Missing, MsAccess.AcPrintQuality.acHigh, Type.Missing, Type.Missing);            

            app.DoCmd.OutputTo(MSAccess.AcOutputObjectType.acOutputReport, "", "Rich Text Format (*.rtf)", rtf, missingValue, missingValue, missingValue);
            app.CloseCurrentDatabase();
            app.Quit(MSAccess.AcQuitOption.acQuitSaveAll);
            Marshal.FinalReleaseComObject(app);
            app = null;
            MSWord.Application word = new MSWord.Application();
            word.Visible = false;
            word.ScreenUpdating = false;
            MSWord.Document doc = word.Documents.Open(rtf, ref missingValue,
            ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
            ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
            ref missingValue, ref missingValue, ref missingValue, ref missingValue);
            doc.Activate();
            string pdfFileName = Path.ChangeExtension(rtf, Guid.NewGuid().ToString().Replace("-", "") + ".pdf");
            object fileFormat = MSWord.WdSaveFormat.wdFormatPDF;
            doc.SaveAs(pdfFileName,
            ref fileFormat, ref missingValue, ref missingValue,
            ref missingValue, ref missingValue, ref missingValue, ref missingValue,
            ref missingValue, ref missingValue, ref missingValue, ref missingValue,
            ref missingValue, ref missingValue, ref missingValue, ref missingValue);
            doc = null;
            ((MSWord.Application)word).Quit(ref missingValue, ref missingValue, ref missingValue);
            Marshal.FinalReleaseComObject(word);
            word = null;
            
            //System.IO.File.Delete(rtf);
            
            return File(pdfFileName, "application/pdf");
        }
    }
}
